package cn.hill4j.tool.core.obj.reflect;

import java.util.LinkedList;
import java.util.List;

/**
  * ClassName: ClassTools
  * Description: class类反射处理工具
  * @author hillchen
  * Date: 2023/8/8
  * @version: 1.0
  */
public class ClassTools {
    /**
     * @author hillchen
     * Description 根据类名全路径获取所有包名
     * 如：cn.hill4j.Test 类会返回集合["cn","cn.hill4j"]
     * Date: 2023/8/8 22:04
     * @param className 类名全路径
     * @return List
     **/
    public static List<String> getAllPackagesByClassName(String className){
        String packageName = getPackageNameByClassName(className);

        return getAllPackagesByPackageName(packageName);
    }
    /**
     * @author hillchen
     * Description 根据包名全路径获取所有包名
     * 如：cn.hill4j 包会返回集合["cn","cn.hill4j"]
     * Date: 2023/8/8 22:07
     * @param packageName 包名全路径
     * @return List
     **/
    public static List<String> getAllPackagesByPackageName(String packageName){
        String[] shortPackageNames = packageName.split("\\.");
        LinkedList<String> packageNames = new LinkedList<>();

        StringBuilder sb = new StringBuilder();
        for (int i=0;i<shortPackageNames.length;i++){
            if (i != 0){
                sb.append(".");
            }
            packageNames.addFirst(sb.append(shortPackageNames[i]).toString());
        }
        return packageNames;
    }
    /**
     * @author hillchen
     * Description 根据类名全路径获取所属包名
     * 如：cn.hill4j.Test 类会返字符串"cn.hill4j"
     * Date: 2023/8/8 22:08
     * @param className 类名全路径
     * @return String
     **/
    public static String getPackageNameByClassName(String className){
        return className.substring(0,className.lastIndexOf(".") -1);
    }
}
